package com.thssh.ui.lsn03.view;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 研究saveLayer
 *
 * @author zhangyugehu
 * @version V1.0
 * @date 2017/09/18
 * email: zhangyugehu@163.com
 */

public class LayerView extends View {

    private Paint mPaint;

    public LayerView(Context context) {
        this(context, null);
    }

    public LayerView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setLayerType(LAYER_TYPE_SOFTWARE, null);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.parseColor("#FF0000"));
        canvas.drawRect(0, 0, 400, 400, mPaint);
        int layer_1 = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
        mPaint.setColor(Color.parseColor("#00FF00"));
        canvas.drawRect(50, 50, 450, 450, mPaint);
        int layer_2 = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.ALL_SAVE_FLAG);
        mPaint.setColor(Color.parseColor("#0000FF"));
        canvas.drawRect(100, 100, 500, 500, mPaint);

        canvas.restore();

        mPaint.setColor(Color.parseColor("#00FFFF"));
        canvas.drawRect(150, 150, 550, 550, mPaint);

    }
}
